﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Simple.Service;
using Simple.Web.Framework.Infrastructure.Aop;
using Simple.ViewModel;
using Simple.Common.Pager;
using Simple.Common.EasyUICommon;
using Simple.Web.Framework.Infrastructure;
using Simple.Common.Core.Encryption;
using Simple.Entity.Enums;

namespace HBootStrap.Admin.Controllers
{
    public class UserController : BaseController
    {
        private readonly UserService _userService;
        private readonly FormsAuthenticationService _formsAuthenticationService;
        public UserController(UserService userService, FormsAuthenticationService formsAuthenticationService)
        {
            _userService = userService;
            _formsAuthenticationService = formsAuthenticationService;
        }

        [MenuViewFilter(MenuName = "用户管理", ModularName = "基础信息管理", Sort = 1)]
        public ActionResult Index()
        {
            var model = new UserQueryModel();
            model.RoleList = _cacheService.GetRoleList(null);
            return View(model);
        }

        public ActionResult AjaxIndex(SimpleMvcPage mvcPage, UserQueryModel queryModel)
        {
            var model = _userService.GetUsers(mvcPage, queryModel);
            return Json(new PageDataGrid { rows = model, total = model.TotalCount }, JsonRequestBehavior.AllowGet);
        }

        [ActionViewFilter(ActionName = "添加用户", MenuName = "用户管理", Sort = 1, ActionType = ActionTypeEnum.Add)]
        public ActionResult Add()
        {
            UserDto model = new UserDto();
            model.IsActive = true;
            model.RoleList = _cacheService.GetRoleList(null);
            return View(model);
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "添加用户", MenuName = "用户管理", Sort = 1, ActionType = ActionTypeEnum.Add)]
        
        public ActionResult Add(UserDto dto)
        {
            var result = _userService.Add(dto.ToEntity());
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "User");
            }
            return Json(result);
        }

        [ActionViewFilter(ActionName = "编辑用户", MenuName = "用户管理", Sort = 2, ActionType = ActionTypeEnum.Edit)]
        public ActionResult Edit(long id)
        {
            var model = _userService.FindById(id);
            var dto = model.ToDto();
            dto.PassWord = "xxxxxx";
            dto.RoleList = _cacheService.GetRoleList(model.RoleId);
            return View(dto);
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "编辑用户", MenuName = "用户管理", Sort = 2, ActionType = ActionTypeEnum.Edit)]
        
        public ActionResult Edit(UserDto dto, string NewPassword)
        {
            var updateUser = _userService.FindById(dto.Id);
            updateUser.Contact = dto.Contact;
            updateUser.IsActive = dto.IsActive;
            updateUser.RoleId = dto.RoleId;
            updateUser.ShortName = dto.ShortName;
            if (!string.IsNullOrEmpty(NewPassword))
            {
                updateUser.PassWord = SimpleMD5Helper.GetMD5(NewPassword + updateUser.PassWordSalt);
            }
            var result = _userService.Update(updateUser);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "User");
            }
            return Json(result);
        }

        public ActionResult ChangePassWord()
        {
            return View();
        }

        [HttpPost]
        
        public ActionResult ChangePassWord(ChangePassWordDto dto)
        {
            dto.LoginNo = User.Identity.Name;
            if (string.IsNullOrEmpty(dto.LoginNo))
            {
                return Json(SimpleResult.GetTimeOut("登录超时", Url.Action("Login", "Login")));
            }
            var result = _formsAuthenticationService.ChangePassword(dto);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("LoginOut", "Login");
            }
            return Json(result);
        }

        public ActionResult IsRepeatLoginNo(string LoginNo)
        {
            var result = _userService.IsRepeatLoginNo(LoginNo);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [ActionViewFilter(ActionName = "分配权限", MenuName = "用户管理", Sort = 3, ActionType = ActionTypeEnum.Set)]
        public ActionResult DistributionAction(long id)
        {
            ViewData["UserId"] = id;
            return View();
        }

        public ActionResult AjaxGetUserTree(long id)
        {
            var model = _userService.GetMenuTree(id);
            return Json(model, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "分配权限", MenuName = "用户管理", Sort = 3, ActionType = ActionTypeEnum.Set)]
        public ActionResult DistributionAction(UserDistributionActionDto dto)
        {
            var result = _userService.DistributionAction(dto.UserId, dto.MenuIdList);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "User");
            }
            return Json(result);
        }
    }
}
